package com.huwei.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.huwei.controller.valid.PhoneValid;
import com.huwei.entity.UserAccount;
import com.huwei.servie.CodeService;

@RestController
@RequestMapping("/code")
public class CodeController {
	
	@Autowired
	private CodeService codeService;
	
	@PostMapping(value="/imageCheckCode",produces = {"application/json; charset=UTF-8"})
	public String imageCheckCode(@RequestParam(name="checkCodeWidth",required=false) Integer checkCodeWidth 
			,@RequestParam(name="checkCodeHeight",required=false) Integer checkCodeHeight) {
		checkCodeWidth = checkCodeWidth == null ? 200 : checkCodeWidth;
		checkCodeHeight = checkCodeHeight == null ? 80 : checkCodeHeight;
		return codeService.imageCheckCode(checkCodeWidth ,checkCodeHeight);
	}
	
	/**
	 * 请求短信验证码
	 * @return
	 */
	@PostMapping(value="/smsCode",produces = {"application/json; charset=UTF-8"})
	public String requestSmsCode(@Validated({PhoneValid.class}) UserAccount userAccount ,Errors error) {
		return codeService.requestSmsCode(userAccount.getPhone());
	}
}
